﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rapid
{
    public partial class frmShowResults : frmMenustrip
    {
        Dictionary<ETest, Label> dicLabelObservation;
        Dictionary<ETest, Label> dicLabelDrugLevel;
        Dictionary<ETest, Label> dicLabelInterpretation;

        Dictionary<ETestCommon, Label> dicLabelCommonObservation;
        Dictionary<ETestCommon, Label> dicLabelCommonInterpretation;

        Dictionary<ETest, EObservation> dicResultsOriginal = new Dictionary<ETest, EObservation>();
        Dictionary<ETestCommon, EObservationCommon> dicResultsCommonOriginal = new Dictionary<ETestCommon,EObservationCommon>();

        public frmShowResults()
        {
            InitializeComponent();

            #region style for button
            btnCancel.TabStop = false;
            btnCancel.FlatStyle = FlatStyle.Flat;
            btnCancel.FlatAppearance.BorderSize = 0;
            btnCancel.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnPrint.TabStop = false;
            btnPrint.FlatStyle = FlatStyle.Flat;
            btnPrint.FlatAppearance.BorderSize = 0;
            btnPrint.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnExport.TabStop = false;
            btnExport.FlatStyle = FlatStyle.Flat;
            btnExport.FlatAppearance.BorderSize = 0;
            btnExport.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent
            #endregion

            foreach (var result in TestResults.DicResults)
                dicResultsOriginal.Add(result.Key, result.Value.observation);

            foreach (var result in TestResults.DicCommonResults)
                dicResultsCommonOriginal.Add(result.Key, result.Value.observation);


            #region labels for Observation Test
            dicLabelObservation = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Obser},
                {ETest.BAR, lbl_BAR_Obser},

                {ETest.BZO, lbl_BZO_Obser},
                {ETest.COC, lbl_COC_Obser},

                {ETest.MDMA, lbl_MDMA_Obser},
                {ETest.MET, lbl_MET_Obser},

                {ETest.MTD, lbl_MTD_Obser},
                {ETest.OPI, lbl_OPI_Obser},

                {ETest.OXY, lbl_OXY_Obser},
                {ETest.PCP, lbl_PCP_Obser},

                {ETest.TCA, lbl_TCA_Obser},
                {ETest.THC, lbl_THC_Obser}
            };
            #endregion

            #region labels for Drug Level Test
            dicLabelDrugLevel = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Drug},
                {ETest.BAR, lbl_BAR_Drug},

                {ETest.BZO, lbl_BZO_Drug},
                {ETest.COC, lbl_COC_Drug},

                {ETest.MDMA, lbl_MDMA_Drug},
                {ETest.MET, lbl_MET_Drug},

                {ETest.MTD, lbl_MTD_Drug},
                {ETest.OPI, lbl_OPI_Drug},

                {ETest.OXY, lbl_OXY_Drug},
                {ETest.PCP, lbl_PCP_Drug},

                {ETest.TCA, lbl_TCA_Drug},
                {ETest.THC, lbl_THC_Drug}
            };
            #endregion

            #region labels for Interpretation Test
            dicLabelInterpretation = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Inter},
                {ETest.BAR, lbl_BAR_Inter},

                {ETest.BZO, lbl_BZO_Inter},
                {ETest.COC, lbl_COC_Inter},

                {ETest.MDMA, lbl_MDMA_Inter},
                {ETest.MET, lbl_MET_Inter},

                {ETest.MTD, lbl_MTD_Inter},
                {ETest.OPI, lbl_OPI_Inter},

                {ETest.OXY, lbl_OXY_Inter},
                {ETest.PCP, lbl_PCP_Inter},

                {ETest.TCA, lbl_TCA_Inter},
                {ETest.THC, lbl_THC_Inter}
            };
            #endregion


            #region labels for Observation Common Test
            dicLabelCommonObservation = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_Cr_Obser},
                {ETestCommon.Ni, lbl_Ni_Obser},

                {ETestCommon.pH, lbl_pH_Obser},
                {ETestCommon.Bl, lbl_Bl_Obser},

                {ETestCommon.SG, lbl_SG_Obser}
            };
            #endregion

            #region labels for InterPretation Common Test
            dicLabelCommonInterpretation = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_CR_Inter},
                {ETestCommon.Ni, lbl_Ni_Inter},

                {ETestCommon.pH, lbl_pH_Inter},
                {ETestCommon.Bl, lbl_Bl_Inter},

                {ETestCommon.SG, lbl_SG_Inter}
            };
            #endregion


            #region init labels Test from result pre step
            foreach (var lblObser in dicLabelObservation)
                lblObser.Value.Text = TestResults.DicResults[lblObser.Key].GetObservstionName();

            foreach (var lblDrugLevel in dicLabelDrugLevel)
                lblDrugLevel.Value.Text = TestResults.DicResults[lblDrugLevel.Key].GetDrugLevel();

            foreach (var lblInterpretation in dicLabelInterpretation)
                lblInterpretation.Value.Text = TestResults.DicResults[lblInterpretation.Key].GetInterPretation();

            if (TestResults.isMonitoring)
                lbl_MTD_Inter.Text = TestResults.DicResults[ETest.MTD].GetInterPretationMonitoring();
            #endregion

            #region init labels Common Test from result pre step
            foreach (var commonTest in TestResults.DicCommonResults)
            {
                dicLabelCommonObservation[commonTest.Key].Text = commonTest.Value.GetObservstionName();
                dicLabelCommonInterpretation[commonTest.Key].Text = commonTest.Value.GetInterPretation();
            }
            #endregion
        }

        private void frmShowResults_Load(object sender, EventArgs e)
        {
            txtComment.Text = Comment.GetComments();
            //listviewControlResult. = ControlTestResults.ControlTestList;
            //grvControlResults.DataSource = ControlTestResults.ControlTestList;
        }

        private void lbl_Observation_Test_MouseHover(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.FromArgb(242, 242, 242);
            lbl_Observation_Test.BorderStyle = BorderStyle.FixedSingle;
        }

        private void lbl_Observation_Test_MouseLeave(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.White;
            lbl_Observation_Test.BorderStyle = BorderStyle.None;
        }

        private void lbl_Observation_Test_Click(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;

            // get type test of control clicked
            var key_TypeTest = dicLabelObservation.Single(lblOb => lblOb.Value == lbl_Observation_Test).Key;

            // change Observation
            TestResults.DicResults[key_TypeTest].ChangeObservationWithRules();
            lbl_Observation_Test.Text = TestResults.DicResults[key_TypeTest].GetObservstionName();

            // update text DrugLevel
            dicLabelDrugLevel[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetDrugLevel();

            // update text InterPretation
            if (key_TypeTest == ETest.MTD && TestResults.isMonitoring)
                dicLabelInterpretation[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetInterPretationMonitoring();
            else 
                dicLabelInterpretation[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetInterPretation();


            // change color InterPretation
            if (dicResultsOriginal[key_TypeTest] != TestResults.DicResults[key_TypeTest].observation)
                dicLabelInterpretation[key_TypeTest].ForeColor = Color.Red;
            else
                dicLabelInterpretation[key_TypeTest].ForeColor = Color.Black;
        }

        private void lblObservation_CommonTest_MouseHover(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.FromArgb(242, 242, 242);
            lbl_Observation_Test.BorderStyle = BorderStyle.FixedSingle;
        }

        private void lblObservation_CommonTest_MouseLeave(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.White;
            lbl_Observation_Test.BorderStyle = BorderStyle.None;
        }

        private void lblObservation_CommonTest_Click(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;

            // get type test of control clicked
            var key_TypeTest = dicLabelCommonObservation.Single(lblOb => lblOb.Value == lbl_Observation_Test).Key;

            // change Observation
            TestResults.DicCommonResults[key_TypeTest].ChangeObservationWithRules();
            lbl_Observation_Test.Text = TestResults.DicCommonResults[key_TypeTest].GetObservstionName();

            // update text InterPretation
            dicLabelCommonInterpretation[key_TypeTest].Text = TestResults.DicCommonResults[key_TypeTest].GetInterPretation();


            // change color InterPretation
            if (dicResultsCommonOriginal[key_TypeTest] != TestResults.DicCommonResults[key_TypeTest].observation)
                dicLabelCommonInterpretation[key_TypeTest].ForeColor = Color.Red;
            else
                dicLabelCommonInterpretation[key_TypeTest].ForeColor = Color.Black;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            TestResults.Reset();
            this.Hide();
            frmRapidRead _frmRapidRead = new frmRapidRead();
            _frmRapidRead.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string folderpath = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.RootFolder = Environment.SpecialFolder.MyComputer;

            DialogResult dr = fbd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                folderpath = fbd.SelectedPath;
                //MessageBox.Show(folderpath);
                Report.ExportPdf(folderpath);               
            }
        }
    }
}
